<?php

/**
 * 会员注册接口
 * File Name：UserRegister.php
 * File Encoding：UTF-8
 * File New Time：2014-8-7 9:48:40
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class UserRegister {

    //错误密码
    protected $error = NULL;
    //注册信息
    protected $data = array();
    //验证规则
    protected $rule = array(
        array('username', 'required', 'message' => '用户名不能为空！'),
        array('username', 'unique', 'allowEmpty' => false, 'message' => '该用户名已经存在！'),
        array('email', 'required', 'message' => '邮箱不能为空！'),
        array('email', 'unique', 'message' => '您输入的邮件已被使用！'),
        array('email', 'email', 'message' => '您输入的邮件地址无效！'),
        array('password', 'length', 'tooShort' => '密码不能低于6位！', 'min' => 6),
        array('password', 'required', 'message' => '密码不能为空！'),
        array('repassword', 'compare', 'compareAttribute' => 'password', 'message' => '两次输入密码不一致！'),
        array('group_id,type,status,regip,regdate,lastloginip,lastlogintime,kind,sex, province,score,city,year,month,day,alipay,alipay_name,phone,is_lock,is_locklog', 'safe'),
    );

    /**
     * 链接注册服务
     * @staticvar null $handier
     * @return \SnsVerification
     */
    static public function getInstance() {
        static $handier = NULL;
        if (empty($handier)) {
            $handier = new UserRegister();
        }
        return $handier;
    }

    /**
     * 获取最近的一条错误信息
     * @return string
     */
    public function getError() {
        return $this->error;
    }

    /**
     * 注册信息
     * @param type $data
     * @return \UserRegister
     */
    public function data($data = array()) {
        if (empty($data) || !is_array($data)) {
            return $this;
        }
        $this->data = $data;
        return $this;
    }

    /**
     * 注册
     * @param type $username 用户名
     * @param type $password 明文密码
     * @param type $repassword 确认密码
     * @param type $email 邮箱
     * @param type $status 帐号状态，0未激活，1已激活
     * @return boolean
     */
    public function register($username = '', $password = '', $repassword = '', $email = '', $status = 0) {
        if (!empty($username)) {
            $this->data['username'] = $username;
        }
        if (!empty($password)) {
            $this->data['password'] = $password;
            $this->data['repassword'] = $repassword;
        }
        if (!empty($email)) {
            $this->data['email'] = $email;
        }
        if (!isset($this->data['status'])) {
            $this->data['status'] = $status;
        }
        unset($this->data['uid']);
        $model = new MembersModel();
        $model->setValidators($this->rule);
        $this->protection();
        $model->attributes = $this->data;
        if (!$model->validate()) {
            $this->error = $model->getOneError() ? $model->getOneError() : '注册信息有误！';
            return false;
        }
        $model->password = $model->hashPassword($model->password);
        $model->repassword = $model->hashPassword($model->repassword);
        $model->regip = GuestDataModel::model()->getForwardedForIp();
        $model->regdate = time();
        if ($model->save()) {
            $this->data['uid'] = $model->primaryKey;
            $this->registerCallback($model);
            //重置
            $this->data = array();
            return $model->primaryKey;
        }
        $this->error = '注册失败！';
        return false;
    }

    /**
     * 邮箱注册
     * @param type $email 用户邮箱
     * @param type $password 密码
     * @param type $repassword 确认密码
     * @param type $status 帐户状态，默认未激活
     * @return boolean
     */
    public function emailRegister($email = '', $password = '', $repassword = '', $status = 0) {
        if (!empty($email)) {
            $this->data['username'] = $email;
            $this->data['email'] = $email;
        }
        if (!empty($password)) {
            $this->data['password'] = $password;
            $this->data['repassword'] = $repassword;
        }
        $this->data['status'] = $status;
        unset($this->data['uid']);
        return $this->register();
    }

    /**
     * 手机号码注册
     * @param type $phone 手机号
     * @param type $emai 邮箱l
     * @param type $password 明文密码
     * @param type $repassword 确认密码
     * @param type $status 帐户状态，默认未激活
     * @return boolean
     */
    public function phoneRegister($phone = '', $email = '', $password = '', $repassword = '', $status = 0) {
        if (!preg_match('/^1[34578][0-9]{9}$/', $phone)) {
            $this->error = '手机号码格式不正确！';
            return false;
        }
        if (!empty($phone)) {
            $this->data['username'] = $phone;
        }
        if (!empty($email)) {
            $this->data['email'] = $email;
        }
        if (!empty($password)) {
            $this->data['password'] = $password;
            $this->data['repassword'] = $repassword;
        }
        $this->data['status'] = $status;
        $this->data['phone'] = $phone;
        unset($this->data['uid']);
        return $this->register();
    }

    /**
     * 用户激活
     * @param type $uid 用户UID
     * @return boolean
     */
    public function userActivate($uid) {
        if (empty($uid)) {
            $this->error = '用户UID不能为空！';
            return false;
        }
        $menber = MembersModel::model()->findByPk($uid);
        if (empty($menber)) {
            $this->error = '该用户不存在！';
            return false;
        }
        $menber->status = 1;
        $menber->save();
        return true;
    }

    /**
     * 设置验证规则
     * @param type $rule 规则，和rule()一样
     * @return boolean
     */
    public function setValidators($rule) {
        if (!empty($rule) && is_array($rule)) {
            $this->rule = $rule;
            return true;
        }
        return false;
    }

    /**
     * 注册回调
     * @param type $model
     * @return boolean
     */
    protected function registerCallback($model) {
        //邀请好友注册送积分
        $invite = (int) Yii::app()->session['invite'];
        if (!empty($invite)) {
            Yii::app()->session->remove('invite');
            MembersInviteModel::model()->addInviteInfo($invite, $model->primaryKey);
            //如果注册就直接激活的立马赠送对应积分
            if ($model->status == 1) {
                MembersInviteModel::model()->sendInvite($invite, $model);
            }
        }
        return true;
    }

    /**
     * 数据保护，这些字段必须通过其他方式进行修改
     * @return boolean
     */
    protected function protection() {
        $notAllowed = array(
            'group_id', 'regip', 'regdate', 'lastloginip', 'lastlogintime', 'score', 'is_lock', 'is_locklog'
        );
        foreach ($notAllowed as $r) {
            unset($this->data[$r]);
        }
        return true;
    }

}
